package com.minshenglife.zanzan.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.BettingDao;
import com.minshenglife.zanzan.dao.IntegralDao;
import com.minshenglife.zanzan.dao.OperateLogDao;
import com.minshenglife.zanzan.dao.RedFlowerDao;
import com.minshenglife.zanzan.dao.UserDao;
import com.minshenglife.zanzan.entity.Betting;
import com.minshenglife.zanzan.entity.Integral;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.pub.Config;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.result.RedFlowerResult;
import com.minshenglife.zanzan.service.BettingService;


/**
 * 押宝service
 * @author huodd
 *
 */
@Component
@Transactional
public class BettingServiceImpl  implements BettingService {
	Logger log = LoggerFactory.getLogger(BettingServiceImpl.class);
	@Resource
	private BettingDao bettingDao;
	@Resource
	RedFlowerDao flowerDao;
	@Resource
	OperateLogDao operateLogDao;
	@Resource
	private UserDao userDao;
	@Resource
	IntegralDao integralDao;
	
	@Override
	public void add(Betting obj) {
		bettingDao.add(obj);
	}
	
	@Override
	public void update(Betting obj) {
		bettingDao.update(obj);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Betting> findBettingList(String empId, Date startDate,
			Date endDate) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("betManId", empId);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		map.put("state", Keys.BETTING_STATE_ZERO);
		List<Betting> list=bettingDao.findBy(map);
		return list;
	}
	
	@Override
	public void batchBettingResult() {
		log.debug("押宝结果批处理开始运行。。。。。");
		String flowerKing=null;//花王
		String endDate="";
		String startDate="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar c=Calendar.getInstance();
//		endDate=sdf.format(c.getTime());
		c.add(Calendar.DAY_OF_MONTH, -1);
		startDate=sdf.format(c.getTime());
		endDate=startDate;
		try {
			//添加批处理日志，记录开始状态
			//获取昨日的花王
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("from", 0);
			map.put("to", 1);
				//设置查询的时间范围
			map.put("endDate", endDate);
			map.put("startDate", startDate);
			List<RedFlowerResult> rows =flowerDao.findRedFlowerRank(map);
			if(rows==null || rows.size()<=0 ){
				log.debug("获取花王失败,批处理终止");
				return ;
			}
			
			flowerKing=rows.get(0).getAddManId();
			log.debug(sdf.format(c.getTime())+"的花王是："+flowerKing);
			
			//获取昨日所有参与该游戏且猜中花王的人
			Map<String,Object> map1=new HashMap<String,Object>();
			map1.put("byBetManId", flowerKing);
			map1.put("endDate", endDate);
			map1.put("startDate", startDate);
			map1.put("state", Keys.BETTING_STATE_ZERO);
			List<Betting> list=bettingDao.findBy(map1);
			if(list==null || list.size()<=0){
				log.debug("获取昨日所有参与该游戏且猜中花王的人——失败,批处理终止");
				return ;
			}
			String obj=Config.getProperty(Config.BETTING_WINNUM);
			log.debug("猜中花王的，奖励积分数："+obj);
			//循环更新猜中者的中奖状态
			for(Betting bet : list){
				log.debug("更新中奖者的相关信息状态："+bet.getBetManId());
				//循环更新猜中者的中奖状态
				bet.setNum(obj);
				bet.setIsWin(Keys.BETTING_ISWIN_ONE);
				bettingDao.update(bet);
				//循环更新中奖者中奖积分
				User user=userDao.getUserByUserId(bet.getBetManId());
				user.setIntegralNum(user.getIntegralNum()+Integer.parseInt(obj));
				userDao.update(user);
				//循环更新中奖者中奖积分流水
				Integral inte=new Integral(Keys.INTEGRAL_TYPE_FIVE,Integer.parseInt(obj),bet.getBetManId(),Keys.INTEGRAL_STATE_ZERO);
				inte.setState(Keys.INTEGRAL_STATE_ZERO);
				integralDao.add(inte);
			}
			log.debug("押宝结果批处理运行成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
